/***************************************************************************
 *   Copyright (C) 2008 by Eduardo Gurgel Pinho                            *
 *   edgurgel@gmail.com                                                    *
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 3 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 *   This program is distributed in the hope that it will be useful,       *
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
 *   GNU General Public License for more details.                          *
 *                                                                         *
 *   You should have received a copy of the GNU General Public License     *
 *   along with this program; if not, write to the                         *
 *   Free Software Foundation, Inc.,                                       *
 *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
***************************************************************************/

#include "tga.h"

#include "color.h"

#include <cstdio>
#include <cstring>
#include <algorithm>

namespace Sirat {

namespace Common {

Tga::Tga(const unsigned int _width,const unsigned int _height) : Image(_width,_height) {
	line_size = _width * 3;
	size = _height * line_size;

	data = new char[size];

}

Tga::Tga() : Image(), size(0), line_size(0), data(0) { }

Tga::Tga(const Tga& _tga) : Image(_tga) {
	
	line_size = width * 3;
	size = height * line_size;
	
	data = new char[size];
	strcpy(data, _tga.data);
}

Tga::~Tga() {
	delete[] data;
}

void Tga::save_file(const char* _file) {
	FILE *file;
	file = fopen(_file,"w+b");
	putc(0,file);
	putc(0,file);
	putc(2,file);/* RGB no compress*/
	putc(0,file); putc(0,file);
	putc(0,file); putc(0,file);
	putc(0,file);
	putc(0,file); putc(0,file);/* X origin */
	putc(0,file); putc(0,file);/* Y origin */
	putc((width & 0x00FF),file);
	putc((width & 0xFF00) / 256,file);
	putc((height & 0x00FF),file);
	putc((height & 0xFF00) / 256,file);
	putc(24,file); /* 24 bit bitmap */
	putc(0,file);

	/* Saving */
	int py,px;
	for(unsigned int y = 0;y < height; y++) {
		py = y*line_size;
		for (unsigned int x = 0; x < width; x++){
			
			/*
			 * Converting Color into chars
			 */
			double r = pixels[x][y].red;
			double g = pixels[x][y].green;
			double b = pixels[x][y].blue;
			unsigned int pixel_x = x;
			unsigned int pixel_y = y;
	
			if (pixel_x > width-1) pixel_x = width-1;
	
			if (pixel_y > height-1) pixel_y = height-1;
	
			int offset = pixel_y*line_size + pixel_x*3;
	
			int ir = (int) (r * 255.0);
			int ig = (int) (g * 255.0);
			int ib = (int) (b * 255.0);
	
			char cr = std::max(0,std::min(ir,255));
			char cg = std::max(0,std::min(ig,255));		
			char cb = std::max(0,std::min(ib,255));
	
	
			data[ offset + 0 ] = cr;
			data[ offset + 1 ] = cg;
			data[ offset + 2 ] = cb;
			
			/*
			 * Saving pixel
			 */
			
			px = x*3;
			putc((data[py+px+2]),file);
			putc((data[py+px+1]),file);
			putc((data[py+px]),file);
		}
	}
	fclose(file);
}

void Tga::load_file(const char* _file) { }

Tga& Tga::operator=(const Tga& _tga) {
	Image::operator=(_tga);
	line_size = width * 3;
	size = height * line_size;

	data = new char[size];
	strcpy(data, _tga.data);
	return *this;
}

Tga* Tga::operator+(const Tga* const _tga) const {
	Tga* result = new Tga(height,width);
		
	Color** result_pixels = Image::operator+(_tga);
	
	for(unsigned int x = 0;x < width; x++)
		for (unsigned int y = 0; y < height; y++)
			result->pixels[x][y] = result_pixels[x][y];
	return result;
}

}

}
